/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package szachy;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Robert
 */
public class Goniec extends Figura implements Serializable{
    private static final long serialVersionUID = 1L;
    public Goniec(){
    }
    public Goniec(char kolor, String nazwa, String rodzaj){
        this.kolor=kolor;
        iloscRuchu=8;
        this.nazwa=nazwa;
        this.rodzaj=rodzaj;
    }
 public boolean mozliwyRuch(int fromX, int fromY, int toX, int toY,IGra g){
     boolean answer=true;
     if(Math.abs(fromX-toX)==Math.abs(fromY-toY)){ //po co w tym warunku było  && fromX!=0 skorko można do tył się poruszać
         if(fromX>toX){
             if(fromY>toY){ 
                 int y=fromY-1;
                 for(int i=fromX-1;i>=toX;i--){
                        try {
                            if (g.CzyZajete(i, y)) {
                                answer = false;
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(Goniec.class.getName()).log(Level.SEVERE, null, ex);
                        }
                     y--;
                 }
             }else{
                 int y=fromY+1;
                 for(int i=fromX-1;i>=toX;i--){
                        try {
                            if (g.CzyZajete(i, y)) {
                                answer = false;
                            }
                        } catch (RemoteException ex) {
                            Logger.getLogger(Goniec.class.getName()).log(Level.SEVERE, null, ex);
                        }
                     y++;
                 }
             
             }
         }else{
              if(fromY>toY){ 
                 int y=fromY-1;
                 for(int i=fromX+1;i<=toX;i++){
                        try {
                            if (g.CzyZajete(i, y)) {
                                answer = false;
                            }
                            y--;
                        } catch (RemoteException ex) {
                            Logger.getLogger(Goniec.class.getName()).log(Level.SEVERE, null, ex);
                        }
                 }
             }else{
                 int y=fromY+1;
                 for(int i=fromX+1;i<=toX;i++){
                        try {
                            if (g.CzyZajete(i, y)) {
                                answer = false;
                            }
                            y++;
                        } catch (RemoteException ex) {
                            Logger.getLogger(Goniec.class.getName()).log(Level.SEVERE, null, ex);
                        }
                 }
             
             }
         }
     }else //niewykonalny ruch
         answer=false;
     return answer;
 }

 public boolean mozliwoscBicia(int fromX, int fromY, int toX, int toY,IGra g){
     try {
            if (g.ZwrocFigure(toX, toY).CoTo().equals("krol")) {
                return false;
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Pion.class.getName()).log(Level.SEVERE, null, ex);
        }
    if(fromX>toX){
        if(fromY>toY){
            return (mozliwyRuch(fromX, fromY, toX+1, toY+1, g));
       }else{
            return (mozliwyRuch(fromX, fromY, toX+1, toY-1, g));
        }
    } else{//fromX<=toX
        if(fromY>toY){
            return (mozliwyRuch(fromX, fromY, toX-1, toY+1, g));
        }else{
            return (mozliwyRuch(fromX, fromY, toX-1, toY-1, g));
        }
    }
}
}
